package com.swfee.qinben;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.active.AppointmentModel;
import com.swfee.qinben.dto.rsp.AppointmentRspDto;
import com.swfee.qinben.httpactions.AppointmentAction;
import com.swfee.qinben.util.CurenActivitUtils;

public class AppointmentListActivity extends AppBaseActivity {

	private LayoutInflater inflater;
	Handler handler = new Handler();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.appointment_list_main);
		
		
		Button addApply  =  (Button) findViewById(R.id.add_appointment_apply);
		addApply.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				CurenActivitUtils.startActivity(AppointmentApplyActivity.class, false);
			}
		});
		asynInitData();
		

	}
	
	
	public void asynInitData() {

		showProgressDialog("加载列表数据，请稍候..");
		new Thread(
				new Runnable() {
					
					@Override
					public void run() {
						AppointmentAction action = new AppointmentAction();
						List<AppointmentRspDto> respDtos = action.getAllAppointments();
						//
						// 加载数据
						//
						final List<AppointmentModel> list = new ArrayList<AppointmentModel>();
						for (int i = 0; i < respDtos.size(); i++) {
							AppointmentRspDto dto = respDtos.get(i);
							list.add(new AppointmentModel(dto.getAppointmentDate(), dto.getFamilyName(),dto.getDoctorName(), dto.getStatus()));
						}

		

						handler.post(new Runnable() {
							
							@Override
							public void run() {
								hideProgressDialog();
								ListView listView = (ListView) findViewById(R.id.listView);

								AppointmentModelListAdapter adapter = new AppointmentModelListAdapter(
										list);

								listView.setAdapter(adapter);
								
							}
						});
						

					}
				}
				).start();
		
	}

	// 适配器
	private final class AppointmentModelListAdapter extends BaseAdapter {

		private List<AppointmentModel> list;

		AppointmentModelListAdapter(List<AppointmentModel> list) {
			this.list = list;

		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.appointment_list_item,null);
				AppointmentModel model = list.get(position);
				CurenActivitUtils.setTextViewContent(R.id.time, model.getSubmitTime(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.doctor, model.getDoctor(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.patient, model.getPatient(), convertView);
				if(model.getStatus()!=1){
					CurenActivitUtils.setTextViewContent(R.id.status, model.getStatusMsg(), convertView,R.color.status_processing);
				}else{
					CurenActivitUtils.setTextViewContent(R.id.status, model.getStatusMsg(), convertView);
				}
				

			}
			return convertView;
		}

	}

}